package cate.game.role.farm.forbidden;

import cate.common.table.d.GDForbidden;
import cate.common.table.farm.forbidden.row.ForbiddenCombatRow;
import cate.common.table.farm.forbidden.row.ForbiddenMonsterRow;
import cate.common.table.farm.forbidden.row.ForbiddenRoomRow;
import cate.common.util.TimeTool;
import cate.common.util.XT;
import cate.game.GameBody;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.annotation.Transient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ToString
@Slf4j
@NoteClass(value = "不同属性所对应的禁地副本")
public class ForbiddenRoom {
    @Transient
    @JsonIgnore
    transient GameBody game;

    @NoteField(value = "圣殿ID")
    public int id;

    @NoteField(value = "当前boss方案ID")
    public int bossPlanId;

    @NoteField(value = "轮次", detail = "0,1,2")
    public int round;

    @NoteField(value = "怪物方案", detail = "<关卡,怪物配置id>")
    public Map<Integer, Integer> monsterMap;


    public ForbiddenRoom(int roomId, GameBody game) {
        this.id = roomId;
    }

    public ForbiddenRoom() {
    }

    public void initialize(GameBody game) {
        this.game = game;
        if (monsterMap == null) {
            monsterMap = new HashMap<>();
        }
        if (monsterMap.isEmpty()) {
            makePlan();
        }
        if (bossPlanId <= 0) {
            makePlan();
        }
    }

    /**
     * 制定boss方案
     */
    private void makePlan() {
        ForbiddenRoomRow tpl = game.table.forbidden.room.get(id);
        if (tpl.bossPlans.length < 3) {
            log.info("种族禁地boss方案配置错误");
            return;
        }
        this.bossPlanId = tpl.bossPlans[round];
        this.monsterMap.clear();
        List<ForbiddenCombatRow> list = game.table.forbidden.combat.getListByRoom(id);
        list.forEach(row -> this.monsterMap.put(row.combatNum, randomMonster(row)));

        log.info("{}重新制定BOSS方案{},怪物方案{}", tpl.name, bossPlanId, monsterMap.values());
    }

    /**
     * 根据挑战圣殿类型关卡数以及boss方案返回怪物实例
     *
     * @param row
     * @return
     */
    private int randomMonster(ForbiddenCombatRow row) {
        List<ForbiddenMonsterRow> list = game.table.forbidden.monster.getListByBossGroup(this.bossPlanId, row.monsterPlanGroup);
        if (list == null) {
            return GDForbidden.UNDEFINE_VALUE;
        }
        int planIndex = XT.randomIndex(list.size());
        return list.get(planIndex).id;
    }

    public void onDaySpan() {
        int day = TimeTool.dayOfWeekCn(System.currentTimeMillis());
        ForbiddenRoomRow tpl = game.table.forbidden.room.get(id);
        if (tpl == null) {
            return;
        }
//        if (tpl.freshDays.contains(day)) {
//            makePlan();
//        }
    }

    public void onWeekSpan() {
        makePlan();
    }

    public void resetRound(int round) {
        this.round = round % 3;
    }
}
